package com.xwli.business.test;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: springcloud
 * @description:
 * @author: xwli
 * @create: 2022-05-17 10:30
 **/
public class MplsVpnTransCodeUtils {
    public static Map<String, String> cityCodeToCNMap = new HashMap<String, String>() {{
        put("3600", "南昌市");
        put("3601", "新余市");
        put("3602", "上饶市");
    }};

    /**
    * @description: 利用apache的MapUtils工具类去反转Map
    * @params: [enCodeMap]
    * @return: java.util.Map<java.lang.String,java.lang.String>
    * @author: xwli
    * @date: 2022/5/17 13:04
    */
    public static Map<String, String> reEnCodeToCNMapFunc(Map<String, String> enCodeMap){
        return MapUtils.invertMap(enCodeMap);
    }

    /**
    * @description: JDK8新特性-利用Stream的方式去反转Map
    * @params: [enCodeMap]
    * @return: java.util.Map<java.lang.String,java.lang.String>
    * @author: xwli
    * @date: 2022/5/17 13:03
    */
    public static Map<String, String> reEnCodeToCNMapFuncTwo(Map<String, String> enCodeMap){
        return enCodeMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
    }

    public static String getEnOrCnDodeMapValue(Map<String,String> enOrCnCodeMap,String key) {
        if (StringUtils.isEmpty(enOrCnCodeMap.get(key))) {
            return "";
        }
        return enOrCnCodeMap.get(key);
    }

    public static void main(String[] args) {
        System.out.println(cityCodeToCNMap.toString());
        System.out.println(MplsVpnTransCodeUtils.reEnCodeToCNMapFunc(cityCodeToCNMap));
        System.out.println(MplsVpnTransCodeUtils.reEnCodeToCNMapFuncTwo(cityCodeToCNMap));
        System.out.println(MplsVpnTransCodeUtils.getEnOrCnDodeMapValue(cityCodeToCNMap,"3601"));
    }
}
